﻿using System;
using System.Windows;
using System.Windows.Controls;
using DasherBci;
using Microsoft.Win32;

namespace WpfView
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow
    {
        private readonly EmotivController _emotivController;
        private readonly DasherController _dasherController;

        public MainWindow()
        {
            InitializeComponent();

            try
            {
                _emotivController = new EmotivController();
                _dasherController = new DasherController();
            }
            catch (DasherBciException e)
            {
                MessageBox.Show("There was a problem starting DahserBCI. Please read the readme file.\n\n" + e.ErrorCode.ToString());
            }
            catch (DllNotFoundException e)
            {
                MessageBox.Show("There was a problem starting DasherBCI. Please read the readme file.\n\n" + e.Message);
            }

            _emotivController.Connect();
        }

        private void buttonLoad_Click(object sender, RoutedEventArgs e)
        {
            if(_emotivController.ConnectedUsersCount == 0)
            {
                MessageBox.Show("A headset must be connected first.");
                return;
            }

            var dialog = new OpenFileDialog();
            var openedFile = (bool) dialog.ShowDialog();
            if (openedFile)
            {
                _emotivController.LoadProfile(dialog.FileName);
            }
        }

        private void buttonSave_Click(object sender, RoutedEventArgs e)
        {
            if (_emotivController.ConnectedUsersCount == 0)
            {
                MessageBox.Show("A headset must be connected first.");
                return;
            }

            var dialog = new SaveFileDialog();
            var savedFile = (bool) dialog.ShowDialog();
            if (savedFile)
            {
                _emotivController.SaveProfile(dialog.FileName);
            }
        }

        private void buttonNew_Click(object sender, RoutedEventArgs e)
        {
            _emotivController.Disconnect();
            _emotivController.Connect();
        }

        private void buttonTrain_Click(object sender, RoutedEventArgs e)
        {
            if (_emotivController.ConnectedUsersCount == 0)
            {
                MessageBox.Show("A headset must be connected first.");
                return;
            }

            buttonTrain.IsEnabled = false;
            var item = (ComboBoxItem) comboBoxAction.SelectedValue;
            TrainedAction action;
            if(Enum.TryParse((string) item.Content, out action))
            {
                _emotivController.AttemptTraining(action);
                Console.Beep();
            }
            buttonTrain.IsEnabled = true;
        }

        private void buttonStart_Click(object sender, RoutedEventArgs e)
        {
            if (_emotivController.ConnectedUsersCount == 0)
            {
                MessageBox.Show("A headset must be connected first.");
                return;
            }

            buttonStop.IsEnabled = true;
            buttonStart.IsEnabled = false;
            _emotivController.NewEegData += _emotivController_NewEegData;
            _dasherController.UpdateVector(_dasherController.VectorOrigin);
            _emotivController.BeginReadingData();
        }

        private void _emotivController_NewEegData(object sender, EegDataEventArgs e)
        {
            CoordinatePair coordinates = EegCoordinateCalculator.CalculateCoordinates(e.Action, e.ActionPower, _dasherController.CurrentPosition);
            _dasherController.UpdateVector(coordinates);
        }

        private void buttonStop_Click(object sender, RoutedEventArgs e)
        {
            buttonStart.IsEnabled = true;
            buttonStop.IsEnabled = false;
            _emotivController.NewEegData -= _emotivController_NewEegData;
            _emotivController.EndReadingData();
        }

        private void buttonReset_Click(object sender, RoutedEventArgs e)
        {
            _dasherController.UpdateVector(_dasherController.VectorOrigin);
        }
    }
}
